//
//  BaseViewController.swift
//  新闻
//
//  Created by 芮京 on 2018/12/11.
//  Copyright © 2018 芮京. All rights reserved.
//

import UIKit
import Alamofire
class BaseViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    let TOUTIAO_PATH = "http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html"
    let TOUTIAO_KEY = "T1348647853363"
    var tab : UITableView?
    var Arr:NSArray = []
    var dic:NSDictionary = [:]
    var KEY:String?
    var url:String?
    override func viewDidLoad() {
        super.viewDidLoad()
        setUrl()
        loadData()
        tabvew()
    }
    //表格
    func tabvew() {
        tab = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: .plain);
        tab?.delegate=self
        tab?.dataSource=self
        self.tab?.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.tab?.register( UINib(nibName: "BaseTableViewCell", bundle: nil), forCellReuseIdentifier: "cells")
        self.view.addSubview(tab!)
    }
    //给url赋值
    func setUrl() {
        self.url = TOUTIAO_PATH
        self.KEY = TOUTIAO_KEY
    }
    //请求
    func loadData() {
        Alamofire.request(url!, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if(response.error == nil){
                print("请求成功")
                self.dic = (response.result.value as? NSDictionary)!
                self.Arr = self.dic.object(forKey: self.KEY!) as! NSArray
                self.tab?.reloadData()

            }else{
                print("请求失败\(String(describing: response.error))")
            }
            //解析网址
        }
    }
    //分行
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.Arr.count
    }
    //行内容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
        
        if indexPath.row == 0{
            //图片
            let img = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 180))
            cell.addSubview(img)
            let Osdic:NSDictionary = Arr[indexPath.row] as! NSDictionary
            let StrImagV = Osdic .object(forKey: "imgsrc") as! String
            let urlStr = NSURL(string:StrImagV )
            let Data = NSData(contentsOf: urlStr! as URL)
            img.image = UIImage(data: Data! as Data )
            
            let Olabel = UILabel(frame: CGRect(x: 10, y: 180, width: UIScreen.main.bounds.size.width, height: 30))
            cell.addSubview(Olabel)
            Olabel.text = Osdic .object(forKey: "title") as? String
            
            
        }else{
            let cell:BaseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! BaseTableViewCell
            let name:NSDictionary = Arr[indexPath.row] as! NSDictionary
            let md:Model =  Model()
            md.dicc = name
            cell.setCellWithData(model: md)
            return cell
        }
        return cell
    }
    //表格高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 210
        }else{
            return 100
        }
    }
    //跳转
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            
        }else{
            let Baseweb = BaswedViewController()
            let nsdic:NSDictionary = Arr[indexPath.row] as! NSDictionary
            Baseweb.str = nsdic.object(forKey: "url") as! String
            self.navigationController?.pushViewController(Baseweb, animated: true);
            
            
        }
    }
    
    
}
